package com.haust.su.ghyy.hospital.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.cmn.client.DictFeignClient;
import com.haust.su.ghyy.hospital.repository.Hospitalrepository;
import com.haust.su.ghyy.hospital.service.HospitalService;
import com.haust.su.ghyy.model.hosp.Hospital;
import com.haust.su.ghyy.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author haustsusu
 * @create 2023-04-26-12:19
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private Hospitalrepository hospitalrepository;


    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public void save(Map<String, Object> parammap) {
        //把参数map集合转换为hospital对象
        String mapString = JSONObject.toJSONString(parammap);
        Hospital hospital = JSONObject.parseObject(mapString, Hospital.class);

        //判断是否存在数据
        String hoscode = hospital.getHoscode();
           //从MongoDB中查出的hospital信息
        Hospital hospitalExist=hospitalrepository.getHospitalByHoscode(hoscode);


        //如果存在,进行修改
        if(hospitalExist!=null){
            hospital.setStatus(hospitalExist.getStatus());
            hospital.setCreateTime(hospitalExist.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospital.setId(hospitalExist.getId());
            hospitalrepository.save(hospital);
        }else{//如果不存在,进行添加
            hospital.setStatus(0);
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(0);
            hospitalrepository.save(hospital);

        }



    }

    //
    @Override
    public Hospital getByHoscode(String hoscode) {
        Hospital hospitalByHoscode = hospitalrepository.getHospitalByHoscode(hoscode);
        return hospitalByHoscode;
    }

    //医院列表(条件查询带分页)
    @Override
    public Page selectHospPage(Integer page, Integer limit, HospitalQueryVo hospitalQuery) {
        //创建pageable的对象
        Pageable pageable= PageRequest.of(page-1,limit);

        //创建example对象
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)//进行模糊查询
                .withIgnoreCase(true);//查询中忽略大小写

        //hospitalqueryvo转换为hospital对象
        Hospital hospital=new Hospital();
        BeanUtils.copyProperties(hospitalQuery,hospital);
        //创建对象
        Example<Hospital> example = Example.of(hospital, matcher);
        //调用方法实现查询
        Page<Hospital> pages = hospitalrepository.findAll(example, pageable);


        //获取查询list集合,遍历进行医院等级的封装
        pages.getContent().stream().forEach(item->{
            this.setHospitalHosType(item);
        });
        return pages;
    }


    //更新医院的上线状态
    @Override
    public void updateStatus(String id, Integer status) {
        //根据id查询出hospital对象
        Optional<Hospital> byId = hospitalrepository.findById(id);
        Hospital hospital = byId.get();

        //设置医院status的值
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());

        hospitalrepository.save(hospital);


    }

    //展示医院详细信息
    @Override
    public Map<String,Object> getHospById(String id) {
        Map<String,Object> Result=new HashMap<>();
        Hospital hospital = this.setHospitalHosType(hospitalrepository.findById(id).get());

        //医院基本信息,包括医院等级
        Result.put("hospital",hospital);
        //单独处理更直观
        Result.put("bookingRule",hospital.getBookingRule());
        //不需要重复返回??
        hospital.setBookingRule(null);
        return Result;
    }


    //根据医院的编号获取医院的名称
    @Override
    public String getHospName(String hoscode) {
        Hospital hospitalByHoscode = hospitalrepository.getHospitalByHoscode(hoscode);
        if (hospitalByHoscode!=null){
            return hospitalByHoscode.getHosname();
        }
        return null;
    }


    //根据医院名称查询
    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalrepository.findHospitalByHosnameLike(hosname);
    }


    //根据医院编号获取医院预约挂号的详情
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.setHospitalHosType(this.getByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;

    }

    //获取查询list集合,遍历进行医院等级的封装
    private Hospital setHospitalHosType(Hospital hospital) {
        //根据dictcode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName("Hostype", hospital.getHostype());
        System.out.println(hostypeString);
        //查询省市地区
        String province = dictFeignClient.getName(hospital.getProvinceCode());
        String city = dictFeignClient.getName(hospital.getCityCode());
        String DistrictString = dictFeignClient.getName(hospital.getDistrictCode());
        System.out.println(province);


        hospital.getParam().put("fullAddress",province+city+DistrictString);
        //System.out.println(province+city+DistrictString);
        hospital.getParam().put("hostypeString",hostypeString);
        return hospital;
    }
}
